import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:freedom/entity/restful.dart';
import 'package:freedom/page/mahjong/common/mahjong_context.dart';
import 'package:freedom/page/mahjong/common/message_content.dart';
import 'package:freedom/page/mahjong/event/user_reflash_action.dart';
import 'package:freedom/page/mahjong/mahjong_game.dart';
import 'package:freedom/util/api_router.dart';
import 'package:freedom/util/http.dart';
import 'package:freedom/widget/current_game_commons.dart';

class MultipleChoiceCardDialog extends StatefulWidget {
  final MahjongIndexGame game;
  const MultipleChoiceCardDialog({super.key, required this.game});

  @override
  MultipleChoiceCardDialogState createState() =>
      MultipleChoiceCardDialogState();
}

class MultipleChoiceCardDialogState extends State<MultipleChoiceCardDialog> {
  @override
  void initState() {
    super.initState();
  }

  MahjongIndexGame get game => widget.game;

  @override
  Widget build(BuildContext context) {
    return Positioned(
      top: 0,
      right: 0,
      bottom: 0,
      left: 0,
      child: Material(
        color: Colors.transparent,
        child: Stack(
          children: [
            InkWell(
              onTap: () {
                game.overlays.remove(MahjongOverlay.multipmleChoiceCardDialog);
              },
              child: Container(
                  decoration: BoxDecoration(
                color: const Color.fromARGB(138, 0, 0, 0),
                borderRadius: BorderRadius.circular(10),
              )),
            ),
            Center(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: game.multiCardList
                        ?.map((e) => buildSelectCard(e))
                        .toList() ??
                    [],
              ),
            )
          ],
        ),
      ),
    );
  }

  Widget buildSelectCard(int cardCode) {
    return InkWell(
      onTap: () {
        var data = MessageContent<MahjongMessage>(
                type: MessageContent.MT_ACT,
                body: MahjongMessage(
                    type: game.multiCardActionCode, cardCode: cardCode))
            .toJson();
        Http.getInstance().request<Restful>(ApiRouter.roomMessage,
            contentType: ContentType.json,
            type: RequestEnum.POST,
            showErrorMsg: true,
            data: data, success: (data) {
          game.overlays.remove(MahjongOverlay.multipmleChoiceCardDialog);
          MahjongContext.postUserEvent<UserReflashActionEvent>(
              MahjongContext.currentUser!.userId, UserReflashActionEvent([]));
        });
      },
      child: Container(
        width: 40.h,
        height: 60.h,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(2.r)),
          border: Border.all(
            color: const Color.fromARGB(255, 224, 196, 37), // 设置边框颜色为金色
            width: 2.0.h, // 设置边框宽度
          ),
        ),
        child: Center(
          child: CardWidget.buildBottomCard(cardCode, 40.h),
        ),
      ),
    );
  }
}
